﻿using Sys.Common;
using Sys.Core.Sys;
using Sys.Model;
using System;
using System.Linq;
using System.Collections.Generic;

namespace Sys.Api.Sys
{
    public class UserApi : BaseApi
    {
        public string Grid(int rows, int page, string search, string departmentid, string sort, string order)
        {
            var entities = UserBll.I.Entities;

            if (!string.IsNullOrEmpty(search))
            {
                entities = entities.Where(m => m.Name.Contains(search));
            }

            if (!string.IsNullOrEmpty(departmentid) && !departmentid.Equals("Null"))
            {
                entities = entities.Where(m => m.DepartmentID.Equals(departmentid));
            }

            var list = UserBll.I.EntitiesPage(entities, rows, page, sort, order);

            var select = list.Select(m => new
            {
                ID = m.ID,
                UserName = m.UserName,
                Name = m.Name,
                Mobile = m.Mobile,
                Tel = m.Tel,
                Email = m.Email,
                IsSuper = m.IsSuper,
                IsVisible = m.IsVisible,
                InsertTime = m.InsertTime
            });

            return GetGrid(entities.Count(), select.ToList());
        }

        public string Save(SysUser model)
        {
            JsonMsg result = new JsonMsg();

            if (string.IsNullOrEmpty(model.ID))
            {
                result.Message = "添加成功!";

                model.ID = StringHelper.GetGuid();

                model.InsertTime = DateTime.Now;
                model.PassWord = StringHelper.GetPassWord(model.PassWord);

                result.Success = UserBll.I.Insert(model) > 0;
            }
            else
            {
                result.Message = "修改成功!";

                SysUser old = UserBll.I.GetByKey(model.ID);

                model.UserName = old.UserName;
                model.PassWord = old.PassWord;

                result.Success = UserBll.I.Update(model) > 0;
            }

            if (!result.Success) { result.Message = "操作失败!"; }

            return JsonHelper.ToJson(result);
        }

        public string Delete(string id)
        {
            JsonMsg result = new JsonMsg();

            result.Success = UserBll.I.Delete(id) > 0;

            if (result.Success) { result.Message = "删除成功!"; }

            return JsonHelper.ToJson(result);
        }

        public string EnableOrDisable(string id, bool bit)
        {
            JsonMsg result = new JsonMsg();

            SysUser model = UserBll.I.GetByKey(id);

            if (model != null)
            {
                if (model.IsVisible != bit)
                {
                    model.IsVisible = bit;

                    result.Success = UserBll.I.Update(model) > 0;

                    if (result.Success)
                    {
                        result.Message = bit ? "启用成功!" : "禁用成功!";
                    }
                }
                else
                {
                    result.Message = bit ? "已经是启用状态!" : "已经是禁用状态!";
                }
            }

            return JsonHelper.ToJson(result);
        }

        public void Export()
        {
            var entities = UserBll.I.Entities.OrderBy(m => m.InsertTime);

            var columns = new List<string[]>();

            columns.Add(new string[] { "UserName", "用户名" });
            columns.Add(new string[] { "Name", "姓名" });
            columns.Add(new string[] { "Mobile", "手机" });
            columns.Add(new string[] { "Tel", "电话" });
            columns.Add(new string[] { "Email", "邮箱" });
            columns.Add(new string[] { "InsertTime", "注册时间" });

            NopiHelper.Export(columns, entities.ToList());
        }

        public string Assign(string id, string data)
        {
            JsonMsg result = new JsonMsg();

            result.Success = UserRoleBll.I.Assign(id, data);

            if (result.Success) { result.Message = "分配成功!"; }

            return JsonHelper.ToJson(result);
        }

        public string RoleGrid(string id)
        {
            var entities = UserRoleBll.I.EntitiesByUserID(id);

            return JsonHelper.ToJson(entities.ToList());
        }

        public string Purview(string id, string data)
        {
            JsonMsg result = new JsonMsg();

            result.Success = UserMenuButtonBll.I.SetPurview(id, data);

            if (result.Success) { result.Message = "设置成功!"; }

            return JsonHelper.ToJson(result);
        }

        public string SetPwd(SysUser model)
        {
            JsonMsg result = new JsonMsg();

            string pwd = StringHelper.GetPassWord(model.PassWord);

            model = UserBll.I.GetByKey(model.ID);

            model.PassWord = pwd;

            result.Success = UserBll.I.Update(model) > 0;

            if (result.Success) { result.Message = "重置成功!"; }

            return JsonHelper.ToJson(result);
        }

        public string EditPwd(SysUser model, string NewPwd)
        {
            JsonMsg result = new JsonMsg();

            string oldpwd = StringHelper.GetPassWord(model.PassWord);

            model = UserBll.CurrentUser;

            if (oldpwd.Equals(model.PassWord))
            {
                model.PassWord = StringHelper.GetPassWord(NewPwd);

                result.Success = UserBll.I.Update(model) > 0;

                if (result.Success) { result.Message = "修改成功!"; }
            }
            else { result.Message = "旧密码不正确!"; }

            return JsonHelper.ToJson(result);
        }
    }
}
